<?php
namespace app\sys\controller;
use app\rbac\model\AdminM;
use app\sys\model\SlideM;
use think\Db;
use think\Session;
use app\base\controller\Base;

class Slide extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 新增
     * @return mixed
     */
    public function add()
    {

        if($this->request->isPost())
        {
            $data = $this->request->post();
            $model = new SlideM();
            $res = $model->add($data);
            if($res)
            {
                $this->success('新增成功',url('sys/Slide/lst'));
            }
            else
            {
                $this->error($model->msg);
            }
        }
        $this->assign(array(
            '_page_title'=>'添加轮播图',
            '_page_title_english'=>'Slide add',
            '_btn_title'=>'轮播图列表',
            '_btn_link'=>url('lst'),
            '_page_ico'=>'iconfa-asterisk',
        ));
        return $this->fetch();
    }



    /**
     * 编辑页面
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        if($this->request->isPost())
        {
            $data = $this->request->post();
            $model = new SlideM();
            $res = $model->edit($data);
            if($res)
            {
                $this->success('修改成功',url('sys/Slide/lst'));
            }
            else
            {
                $this->error($model->msg);
            }
        }
        //获取数据
        $data = Db::table('sys_slide')->where('id',$id)->field('id,sm_pic,name,type,linksrc,sort_num')->find();

        $this->assign(array(
            '_page_title'=>'编辑轮播图',
            '_page_title_english'=>'Slide edit',
            '_btn_title'=>'轮播图列表',
            '_btn_link'=>url('lst'),
            '_page_ico'=>'iconfa-asterisk',
            'data'=>$data,
        ));
        return $this->fetch();
    }

    /**
     * 获取全部数据
     * @return mixed
     */
    public function lst()
    {

        $model = new SlideM();
        $data = $model->lst(15);
        // 获取分页显示
        $page = $data->render();
        $this->assign(array(
            '_page_title'=>'轮播图列表',
            '_page_title_english'=>'Slide list',
            '_btn_title'=>'添加轮播图',
            '_btn_link'=>url('add'),
            '_page_ico'=>'iconfa-asterisk',
            'data'=>$data,
            'page'=>$page,
        ));
        return $this->fetch();
    }


    /**
     * 删除数据
     * @param $id
     */
    public function del()
    {
        $id = $this->request->post('id');
        $model = new SlideM();
        $model->del($id);
        return json(['code'=>$model->code,'msg'=>$model->msg]);
    }
}
